package demo.main;

import static net.sf.dynamicreports.report.builder.DynamicReports.cht;
import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.report;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.style.FontBuilder;
import net.sf.dynamicreports.report.constant.TimePeriod;
import net.sf.dynamicreports.report.exception.DRException;
import net.sf.jasperreports.engine.JRDataSource;

import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

public class TimeSeriesChart extends MutiReport {
	@At("/getTimeSeriesChart")
	@Ok("json")
	public String getTimeSeriesChart(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("getTimeSeriesChart");
		build(request, response);
		return resultResult;
	}
	
	protected void build(HttpServletRequest request, HttpServletResponse response) {
		FontBuilder boldFont = stl.fontArialBold().setFontSize(12);

		TextColumnBuilder<Date> orderDateColumn = col.column("日期",
				"orderdate", type.dateYearToMonthType());
		TextColumnBuilder<Integer> quantityColumn = col.column("数量",
				"quantity", type.integerType());
		TextColumnBuilder<BigDecimal> priceColumn = col.column("价格",
				"price", type.bigDecimalType());

		try {
			reportBuilder = report().setTemplate(Templates.reportTemplate)
					.columns(orderDateColumn, quantityColumn, priceColumn)
					.title(Templates.createTitleComponent(""))
					.summary(
							cht.timeSeriesChart()
									//.setTitle("Time series chart")
									.setTitleFont(boldFont)
									.setTimePeriod(orderDateColumn)
									.setTimePeriodType(TimePeriod.MONTH)
									.series(cht.serie(quantityColumn),
											cht.serie(priceColumn))
									.setTimeAxisFormat(
											cht.axisFormat().setLabel("日期")))
					.pageFooter(Templates.footerComponent)
					.setDataSource(createDataSource());

			setResult(reportBuilder.toJasperPrint(), request, response);
		} catch (DRException e) {
			e.printStackTrace();
		}
	}
	
	private JRDataSource createDataSource() {
		DataSource dataSource = new DataSource("orderdate", "quantity", "price");		
		dataSource.add(toDate(2010, 1), 50, new BigDecimal(200));
		dataSource.add(toDate(2010, 2), 110, new BigDecimal(450));
		dataSource.add(toDate(2010, 3), 70, new BigDecimal(280));
		dataSource.add(toDate(2010, 4), 250, new BigDecimal(620));
		dataSource.add(toDate(2010, 5), 100, new BigDecimal(400));
		dataSource.add(toDate(2010, 6), 80, new BigDecimal(320));
		dataSource.add(toDate(2010, 7), 180, new BigDecimal(490));
		return dataSource;
	}
	
	private Date toDate(int year, int month) {
		Calendar c = Calendar.getInstance();
		c.clear();
		c.set(Calendar.YEAR, year);
		c.set(Calendar.MONTH, month - 1);
		return c.getTime();
	}
}
